##Chapter2Master.r
###master script for creating all Tables and Figures 
###in original analysis included in Chapter 2 of:
###Enos, Ryan D.  2017. The Space Between Us:
### Social Geography and Politics
####RdE October 2017

rm(list = ls())

##load packages
library(apsrtable)
library(ggplot2)
library(stargazer)
library(data.table)
library(AER)

##clustered standard errors function
cl   <- function(dat,fm, cluster){
  require(sandwich, quietly = TRUE)
  require(lmtest, quietly = TRUE)
  M <- length(unique(cluster))
  N <- length(cluster)
  K <- fm$rank
  dfc <- (M/(M-1))*((N-1)/(N-K))
  uj  <- apply(estfun(fm),2, function(x) tapply(x, cluster, sum));
  uj = uj[complete.cases(uj),]  
  
  vcovCL <- dfc*sandwich(fm, meat=crossprod(uj)/N)
  return(vcovCL) }

source('TablesA1A2Figure2_1.r')
source('TablesA3A4.r')
source('TablesA5A6.r')
source('TablesA7A8.r')
source('TablesA9A10.r')
source('TablesA11A12.r')
source('TableA13.r')